<?php
/**
 * Status Controller for Congregation Component
 * 
 * @package    Congregation
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4
 * @license		GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

/**
 * Status Controller
 *
 * @package    Congregation
 * @subpackage Components
 */
class CongregationControllerUpdates extends CongregationController
{
	/**
	 * constructor (registers additional tasks to methods)
	 * @return void
	 */
	var $_db;
	
	function __construct()
	{
		parent::__construct();

		// Register Extra tasks
		$this->_db	=& JFactory::getDBO();

		$this->registerTask( 'add'  , 	'edit' );
		$this->registerTask( 'new'  , 	'edit' );
		$this->registerTask( 'orderup',         'order');
		$this->registerTask( 'orderdown',       'order');
	}

	function generatepaths()
	{
		$query = "SELECT username, c_id FROM #__congregation_profile";
		$this->_db->setQuery($query);
		$rows = $this->_db->loadObjectList();
		
		foreach ($rows as $row)
		{
			
			
			$query = "INSERT INTO #__congregation_profile_paths (t_username, t_date, t_cid, t_current) VALUES ('{$row->username}', '2010-01-01', {$row->c_id}, 1)";
			$this->_db->setQuery($query);
			$this->_db->query();
			
		}
	}
	
	
	/*
	function display()
	{
		
		$query = "SELECT DISTINCT c_id FROM #__congregation_profile";
		$this->_db->setQuery($query);
		$this->_db->query();
		$rows = $this->_db->loadObjectList();
		
		foreach ($rows as $row)
		{
			
			$this->_db->setQuery("SELECT p_id FROM #__congregation_casa WHERE c_id={$row->c_id}");
			$p_id = $this->_db->loadResult();
			
			$query = "UPDATE #__congregation_profile SET p_id = {$p_id} WHERE c_id ={$row->c_id}";
			$this->_db->setQuery($query);
			$this->_db->query();
			
		}

		//$this->setRedirect('index.php?option=com_congregation&controller=status' , $msg);
	}
	*/
	
	/*
	function datsotojoom()
	{
		$query = "INSERT INTO #__joomgallery (catid, imgtitle, alias, imgtext, imgdate, ordering, imgfilename, imgthumbname, owner)  SELECT catid, imgtitle, imgtext, imgdate, ordering, imgfilename, imgthumbname, '62' FROM  #__datsogallery";
		$this->_db->setQuery($query);
		$this->_db->query();
		
		$query = "INSERT INTO #__joomgallery_catg (cid, name, alias, parent, description, ordering)  SELECT cid, name, CONCAT(CAST(cid AS CHAR), '-', REPLACE(REPLACE(LOWER(name), ' ', ''), '.', '-')), parent, description, ordering FROM #__datsogallery_catg";
		$this->_db->setQuery($query);
		$this->_db->query();
	}
	*/
	/*
	function joomfix()
	{
		
		$query = "SELECT * FROM #__joomgallery_catg "; //WHERE catpath = ''";
		$this->_db->setQuery($query);
		$rows = $this->_db->loadObjectList();
		
		foreach ($rows as $row)
		{
			//create catpath
			
			$catpath  = strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'),
  array('', '_', ''), $row->name));
			
			//update table
			$query2 = "UPDATE #__joomgallery_catg SET catpath= '" . $catpath . "' WHERE cid={$row->cid}";
			$this->_db->setQuery($query2);
			$this->_db->query();
			
			//create the catpath directory
			
			$catpath = $row->catpath;
			$tpath = '/home/rcj0002/public_html/media/joomgallery/thumbnails/' . $catpath;
			$opath = '/home/rcj0002/public_html/media/joomgallery/originals/' . $catpath;
			$dpath = '/home/rcj0002/public_html/media/joomgallery/details/' . $catpath;
			
			mkdir($tpath);
			mkdir($opath);
			mkdir($dpath);
			
			//copy files to their respective places
			
			$query3 = "SELECT * FROM #__joomgallery WHERE catid = {$row->cid}";
			$this->_db->setQuery($query3);
			$rows3 = $this->_db->loadObjectList();
			foreach ($rows3 as $row3)
			{
				echo "<br/> Copied " . $row3->imgfilename;
				copy('/home/rcj0002/public_html/media/joomgallery/thumbnails/' . $row3->imgthumbname , $tpath . '/' . $row3->imgthumbname);
				copy('/home/rcj0002/public_html/media/joomgallery/originals/' . $row3->imgfilename , $opath . '/' . $row3->imgfilename);
				copy('/home/rcj0002/public_html/media/joomgallery/details/' . $row3->imgfilename , $dpath . '/' . $row3->imgfilename);

			}
			
		}
	}
	*/
	
	
	function fixnecro()
	{
	/*
		$query = "SELECT * FROM #__congregation_necro";
		$this->_db->setQuery($query);
		$rows = $this->_db->loadObjectList();
		
		foreach ($rows as $row)
		{
			$query2 = "SELECT o.ddate, u.name FROM #__users u LEFT JOIN #__congregation_profile_old o ON u.username=o.username WHERE u.username ='{$row->username}' LIMIT 1";
			$this->_db->setQuery($query2);
			$row2 = $this->_db->loadObject();
			$query3 = "UPDATE #__congregation_necro SET name='{$row2->name}', `date` = '{$row2->ddate}' WHERE id={$row->id}";
			$this->_db->setQuery($query3);
			$this->_db->query();
			echo $row2->name . " " . $row2->ddate . "<br />";
		}
	*/
	
	//get ddate for those who don't have necro text
	/*
		$query = "SELECT o.ddate, u.name FROM #__users u LEFT JOIN #__congregation_profile_old o ON u.username=o.username WHERE o.ddate <> '0000-00-00' ";
		$this->_db->setQuery($query);
		$rows = $this->_db->loadObjectList();
		foreach( $rows as $row )
		{
			$query2 = "INSERT IGNORE INTO #__congregation_necro (name, `date`) VALUES ('{$row->name}', '{$row->ddate}')";
			$this->_db->setQuery($query2);
			$this->_db->query();
		}
	*/
	
	//remove defunti from user
	/*
		$query = "SELECT name FROM #__congregation_necro";
		$this->_db->setQuery($query);
		$rows = $this->_db->loadObjectList();
		
		foreach($rows as $row)
		{
			$query = "DELETE FROM #__users WHERE name='{$row->name}' LIMIT 1";
			$this->_db->setQuery($query);
			$this->_db->query();
		}
	*/	

	//remove defunti from profiles
	/*
		$query = "SELECT username FROM #__congregation_necro";
		$this->_db->setQuery($query);
		$rows = $this->_db->loadObjectList();
		
		foreach($rows as $row)
		{
			$query = "DELETE FROM #__congregation_profile WHERE username='{$row->username}' LIMIT 1";
			$this->_db->setQuery($query);
			$this->_db->query();
		}
	*/
	
	
	
	}
	
	function fixprofilepic()
	{
		/*
		//take old images and put it in new site
		$query = "SELECT username, picfile FROM #__congregation_profile_old WHERE picfile <> '' ORDER BY username";
		$this->_db->setQuery($query);
		$rows = $this->_db->loadObjectList();
		//var_dump($rows);
		//exit;
		if($rows)
		{
			$dest_dir = JPATH_ROOT . DS . 'media' . DS . 'congregation'. DS . 'profiles' . DS . 'originals';
			$thumb_dir = JPATH_ROOT . DS . 'media' . DS . 'congregation'. DS . 'profiles' . DS . 'thumbnails';
			foreach ($rows as $row)
			{
				$filename = uniqid('') . $row->username . '.jpg';
				copy ('/home/rcj0002/domains/v1.rcj.org/public_html/media/profiles/b' . $row->picfile, $dest_dir . DS . $filename);
				copy ('/home/rcj0002/domains/v1.rcj.org/public_html/media/profiles/s' . $row->picfile, $thumb_dir . DS . $filename);
				
				$query2 = "INSERT IGNORE INTO #__congregation_profile_photos (username, filename, `date`) VALUES ('{$row->username}', '{$filename}', '2010-09-06')";
				$this->_db->setQuery($query2);
				$this->_db->query();
			}
		}
		
		//only for those who do not have an image
		*/
	}
	
	function fixnecrobis()
	{
		$query = "SELECT username, texte FROM #__congregation_necro_old";
		$this->_db->setQuery($query);
		$rows = $this->_db->loadObjectList();
		
		foreach($rows as $row)
		{
			echo $row->username . "<br>";
			$query2 = "UPDATE #__congregation_necro SET texte = '" . $this->_db->getEscaped($row->texte) . "' WHERE username = '{$row->username}'";
			
			$this->_db->setQuery($query2);
			$this->_db->query();
			
			
		}
	}
}
?>	
<!--
	function fixnecropic()
	{
		/*
		//fix error first tsss
		
		//if username has a pic then put it in necro
		$query = "SELECT id, texte, username FROM #__congregation_necro WHERE username <> ''";
		$this->_db->setQuery($query);
		$rows = $this->_db->loadObjectList();
		if($rows)
		{
		
			foreach($rows as $row)
			{
				/*
				$query3 = "UPDATE #__congregation_necro SET texte = '" .strip_tags($row->texte, '<br><p><i><b>') . "' WHERE id = '{$row->id}'";
				$this->_db->setQuery($query3);
				$this->_db->query();
				*/
				
				$query2 = "SELECT filename FROM #__congregation_profile_photos WHERE username = '{$row->username}' LIMIT 1";
				$this->_db->setQuery($query2);
				$row2 = $this->_db->loadObject();
				
				//var_dump($row2);
				//exit;

				
				
				if($row2)
				{
					
					
					$query3 = "UPDATE #__congregation_necro SET texte = CONCAT('<p><img src=\"http://rcj.org/media/congregation/profiles/originals/" . $row2->filename . "\"/></p>', texte) WHERE id = '{$row->id}' LIMIT 1";
					
					$this->_db->setQuery($query3);
					$this->_db->query();
					
				}
				
				
			}
		}
	}
}
-->